package com.runen.wnhz.runen.ui.activity.login;
import android.content.Intent;
import android.icu.util.VersionInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.common.utils.Constants;
import com.runen.wnhz.runen.common.utils.JumpUtlis;
import com.runen.wnhz.runen.common.utils.NetUtils;
import com.runen.wnhz.runen.common.utils.OkHttpUtils;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.MineBean;
import com.runen.wnhz.runen.data.entity.MyCollection;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.ui.activity.mine.MyCollectionActivity;
import com.runen.wnhz.runen.ui.adapter.puadapter.MyCollectionRecyclerAdapter;
import com.runen.wnhz.runen.widget.TitleBuilder;

import java.io.IOException;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018-04-09.
 */

public class LoginForEnterpriseActivity extends BaseActivity implements View.OnClickListener {
    
    @BindView(R.id.iv_back)
    ImageView iv_back;
    
    @BindView(R.id.tv_forenterpr_next)
    TextView tv_forenterpr_next;    
    
    @BindView(R.id.et_input_phone)
    EditText et_input_phone;    
    
    @BindView(R.id.et_input_password)
    EditText et_input_password;

    @Override
    protected int getLayoutView() {
        return R.layout.login_for_enterprise_activty_layout;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        setToolBarVisible(View.GONE);
    }

    @Override
    protected void initStart() {
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this,R.color.title_green);
        initView();
    }


    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        setToolBarVisible(View.GONE);
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {

    }

    private void initView() {
        iv_back.setOnClickListener(this);
        tv_forenterpr_next.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_forenterpr_next:
                if (TextUtils.isEmpty(et_input_phone.getText().toString().trim())){
                    ToastUtil.showToast("城市编码不能为空");
                } else if (TextUtils.isEmpty(et_input_password.getText().toString().trim())){
                    ToastUtil.showToast("企业识别码不能为空");
                } else {
                    FormBody formBody = new FormBody.Builder().add("city_code",et_input_phone.getText().toString()).add("company_code", et_input_password.getText().toString()).build();
                    Request.Builder builder = new Request.Builder().url(NetUtils.USER_VERIFYCODE).post(formBody);
                    Call newCall = OkHttpUtils.getOkHttpClient().newCall(builder.build());
                    newCall.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtil.showToast("数据请求失败");
                                }
                            });
                        }

                        @Override
                        public void onResponse(Call call, final Response response) throws IOException {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        handleUserCollectionData(response);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                        ToastUtil.showToast("数据请求异常");
                                    }
                                }
                            });
                        }
                    });

                }
                break;
            default:
                break;
        }
    }

    private void handleUserCollectionData(Response response) throws IOException {
        if (response.isSuccessful()) {
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                String json = responseBody.string();
                MineBean myCollection = new Gson().fromJson(json, MineBean.class);
                if ("1".equals(myCollection.getRe())) {
                    Constants.cityCode = et_input_phone.getText().toString().trim();
                    Constants.companyCode = et_input_password.getText().toString().trim();
                    JumpUtlis.getInstance().jumpActivity(this,VerificationActivity.class);
                } else if ("0".equals(myCollection.getRe())) {
                    ToastUtil.showToast(myCollection.getInfo());
                }
            }
        }
    }
    
}
